package com.jxd.petMgr.servlet;

import com.jxd.petMgr.dao.IPetStoreDao;
import com.jxd.petMgr.dao.impl.PetStoreDaoImpl;
import com.jxd.petMgr.model.Pet;
import com.jxd.petMgr.model.PetOwner;
import com.jxd.petMgr.service.IPetOwnerService;
import com.jxd.petMgr.service.IPetService;
import com.jxd.petMgr.service.IPetStoreService;
import com.jxd.petMgr.service.impl.PetOwnerServiceImpl;
import com.jxd.petMgr.service.impl.PetServiceImpl;
import com.jxd.petMgr.service.impl.PetStoreServiceImpl;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/buyServlet")
public class BuyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        IPetService petService = new PetServiceImpl();
        IPetStoreService petStoreService = new PetStoreServiceImpl();
        IPetOwnerService petOwnerService = new PetOwnerServiceImpl();
        String msg = "";
        int totalPrice = 0;//宠物总价格
        //接收前台数据
        String str = request.getParameter("petIds");
        //把字符串分隔为数组
        String []  strPets = str.split(",");
        //转换为int 类型的集合
        List<Integer> petIdList = new ArrayList<>();
        //宠物集合
        List<Pet> petList = new ArrayList<>();
        for (String strPet : strPets) {
            if (strPet != null && !"".equals(strPet)) {
                int id = Integer.parseInt(strPet);
                Pet pet = petService.getPetById(id);
                totalPrice += pet.getPrice();
                petIdList.add(id);
            }
        }

        //判断主人的余额是否充足
        HttpSession session = request.getSession();
        //通过session取到的对象id
        PetOwner master = (PetOwner) session.getAttribute("petOwner");
        int petOwnerId = master.getId();
        PetOwner petOwner = petOwnerService.getOwnerByID(petOwnerId);
        //如果主人余额大于总价则，购买成功，否则提示余额不足
        if(petOwner.getMoney() >= totalPrice){
//            boolean isSucc = false;//是否购买成功
//            List<Boolean> isAllSucc = new ArrayList<>();
            for (int petId:petIdList) {
                Pet pet = petService.getPetById(petId);
                pet.setOwner_id(petOwnerId);
                petList.add(pet);


                //是否购买成功
//                boolean isSucc = petStoreService.sell(pet) && petOwnerService.buy(pet);
//                if (petOwnerService.buy(pet) && petStoreService.sell(pet)){
//                    //当前宠物购买失败
//
//                }
//                isAllSucc.add(isSucc);
            }
//            for (Boolean aBoolean : isAllSucc) {
//                if (!aBoolean) {
//                    msg = "购买失败";
//                    break;
//                }
//            }
//            if (!"购买失败".equals(msg)){
//                msg = "全部购买成功";
//            }
//            isSucc = petStoreService.sellBatch(petList) && petOwnerService.buyBatch(petList);
            if(petStoreService.sellBatch(petList) && petOwnerService.buyBatch(petList)){
                msg = "购买成功";
            } else {
                msg = "购买失败";
            }

        } else {
            msg = "余额不足";
        }
        PrintWriter pw = response.getWriter();
        pw.write(msg);
        pw.flush();
        pw.close();
    }
}
